package pers.baiban.sqltoword.handler;

import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import pers.baiban.sqltoword.config.TableParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 表格边框辅助处理器抽象类
 *
 * @author 强子初
 * @since 2023/11/29
 */
public abstract class AbstractTableProcessor implements TableProcessor {

    protected List<XWPFTableCell> cells;
    protected CTTblBorders borders;
    protected TableParam param;

    @Override
    public void init(XWPFTable table, TableParam param) {
        this.cells = getAllCells(table);
        this.borders = getCTTblBorders(table);
        this.param = param;
    }

    /**
     * 获取表格所有cell
     */
    private List<XWPFTableCell> getAllCells(XWPFTable table) {
        return table.getRows().stream()
                .flatMap(row -> row.getTableCells().stream())
                .collect(Collectors.toList());
    }
}